package com.sxx.beautifulcity.adapter;

import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.activity.PetActivity;
import com.sxx.beautifulcity.entity.PetData;
import com.sxx.beautifulcity.listenerInterface.OnMyItemClickListener;

import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class PetAdapter extends RecyclerView.Adapter<PetAdapter.PetAdapterViewHolder> {

    private List<PetData> list;
    private OnMyItemClickListener listener;


    public void setOnMyItemClic(OnMyItemClickListener l){
        listener = l;
    }

    public PetAdapter() {

    }

    public void addData(List<PetData> list){
        this.list = list;
        notifyDataSetChanged();
    }

    class PetAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private SimpleDraweeView simple_drawePet;
        private TextView pet_nameTv;
        private RatingBar pet_ratingBar;
        private LinearLayout pet_linearlayout;
        private int position;

        public PetAdapterViewHolder(View itemView) {
            super(itemView);
            simple_drawePet = (SimpleDraweeView) itemView.findViewById(R.id.simple_drawePet);
            pet_nameTv = (TextView) itemView.findViewById(R.id.pet_nameTv);
            pet_ratingBar = (RatingBar) itemView.findViewById(R.id.pet_ratingBar);
            pet_linearlayout = (LinearLayout) itemView.findViewById(R.id.pet_linearlayout);
            pet_linearlayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (list != null && list.size() > 0) {
                PetData data = list.get(position);
                if (listener != null) {
                    listener.onMyItemClick(data);
                }
            }
        }
    }

    @Override
    public PetAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.activity_pet_item, parent, false);

        return new PetAdapterViewHolder(v);
    }

    @Override
    public void onBindViewHolder(PetAdapterViewHolder holder, int position) {
        if (list != null && list.size() > 0) {
            holder.pet_nameTv.setText(list.get(position).getName());
            holder.pet_ratingBar.setNumStars(list.get(position).getStar());
            String path = list.get(position).getImage();
            frescoImage(path , holder.simple_drawePet);
        }
        holder.position = position;
    }

    public void frescoImage(String path , SimpleDraweeView draweeView){
        Uri uri = Uri.parse(path);
        draweeView.setImageURI(uri);
    }

    @Override
    public int getItemCount() {
        return list != null && list.size() > 0 ? list.size() : 0;
    }


}
